'use strict';
import React from "react";
import {View, StyleSheet, TouchableOpacityComponent, TouchableOpacity} from "react-native";
import Container from "../../component/common/Container";
import NavigationBar from "../../component/navigation/NavigationBar";
import ImageView from "../../component/common/ImageView";
import FreeForumComponent from "../../component/find/FreeForumComponent";
import ListView from "../../component/list/ListView";

class FreeForumList extends React.PureComponent {

    constructor(props) {
        super(props);
        this.state = {data: []};
        this.page = 1;
        this.limit = 10;
    }

    componentDidMount() {
        this.requestDataSource();
    }

    requestDataSource = async () => {
        const data = {
            page: this.page,
            limit: this.limit
        };
        const result = await Services.post(ServicesApi.FREE_FORUM, data);
        // console.log('自由论坛返回--------->', result);
        if (result.code === StatusCode.SUCCESS_CODE) {
            let dataSource = [];
            if (Array.isArray(result.data)) {
                if (data.page === 1) {
                    dataSource = result.data;
                } else {
                    let temp = this.state.data;
                    dataSource = temp.concat(result.data);

                }
            }
            this.setState({data: dataSource})
        }

        this._listRef && this._listRef.stopRefresh();
        this._listRef && this._listRef.stopEndReached({allLoad: result.data.length < this.limit}); // 是否加载完毕需要自行判断
    };

    _onUpdateForumData = (item, index) => {
        // console.log(index, item);
        let temp = this.state.data.slice();
        temp[index] = item;
        this.setState({data: temp})
    };

    _renderItem = (info) => {
        const {item, index} = info;
        // console.log('render', item);
        return (
            <FreeForumComponent
                item={item}
                index={index}
                onPressDZ={(item, index) => this._onUpdateForumData(item, index)}
            />
        )
    };

    _onRefresh = () => {
        this.page = 1;
        this.requestDataSource();
    };

    _onEndReached = () => {
        this.page++;
        this.requestDataSource();
    };

    _captureRef = (v) => {
        this._listRef = v
    };

    _keyExtractor = (item, index) => {
        return `${index}`
    };

    _onPressPublish = () => {
        // console.log('发帖');
        RouterHelper.navigate('发布帖子', 'PublishNote', {callBack: () => this._onRefresh()})
    };

    render() {
        const {params} = this.props.navigation.state;
        const pageTitle = params.pageTitle || '自由论坛';
        return (
            <Container>
                <NavigationBar
                    title={pageTitle}
                    renderRightAction={[{icon: Images.icon_add_logo, iconStyle:{width:17,height:17,marginRight:5},onPress:() => this._onPressPublish()}]}
                />
                <ListView
                    data={this.state.data}
                    initialRefresh={false}
                    enableRefresh={true}
                    enableLoadMore={true}
                    ref={this._captureRef}
                    style={styles.listView}
                    onRefresh={this._onRefresh}
                    renderItem={this._renderItem}
                    keyExtractor={this._keyExtractor}
                    // exData={this.state.data}
                    onEndReached={this._onEndReached}
                />
            </Container>
        );
    }
}

const styles = StyleSheet.create({
    listView: {},
});

export default FreeForumList